package com.xcyy.clbzbh.service.imp;

import com.xcyy.clbzbh.VO.QueryParam;
import com.xcyy.clbzbh.entity.GPSPoint;
import com.xcyy.clbzbh.repository.GPSPointRepository;
import com.xcyy.clbzbh.service.GPSPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class GPSPointServiceImp implements GPSPointService {
    @Autowired
    private GPSPointRepository gpsPointRepository;
    @Override
    public Page<GPSPoint> findAll(QueryParam queryParam,int pageNum, int pageSize) {
        Pageable pageable= PageRequest.of(pageNum,pageSize, Sort.by(Sort.Direction.ASC,"pointID"));
        Page<GPSPoint> gpsPoints=gpsPointRepository.findAll(new Specification<GPSPoint>() {
            @Override
            public Predicate toPredicate(Root<GPSPoint> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if (queryParam.getName()!=null&&!"".equals(queryParam.getName())){
                    predicates.add(cb.like(root.get("name"),"%"+queryParam.getName()+"%"));
                }
                if (queryParam.getDh()!=null&&!"".equals(queryParam.getDh())){
                    predicates.add(cb.equal(root.get("pointID"),queryParam.getDh()));
                }
                if (queryParam.getGrade()!=null&&!"".equals(queryParam.getGrade())){
                    predicates.add(cb.equal(root.get("grade"),queryParam.getGrade()));
                }
                if (queryParam.getCity()!=null&&!"".equals(queryParam.getCity())){
                    predicates.add(cb.equal(root.get("city"),queryParam.getCity()));
                }
                if (queryParam.getCountry()!=null&&!"".equals(queryParam.getCountry())){
                    predicates.add(cb.equal(root.get("country"),queryParam.getCountry()));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, pageable);
        return gpsPoints;
    }

    @Override
    public GPSPoint save(GPSPoint gpsPoint) {
        return gpsPointRepository.save(gpsPoint);
    }

    @Override
    public GPSPoint findById(String id) {
        return gpsPointRepository.findById(id).orElse(null);
    }

    @Override
    public Object totalgpspoint() {
        return gpsPointRepository.totalgpspoint();
    }

    @Override
    public List<GPSPoint> findByCountry(String country) {
        return gpsPointRepository.findByCountry(country);
    }
}
